package net.bright.demo.run;

import java.util.Map;

import net.bright.demo.common.DictKeys;
import net.bright.demo.ext.beetl.MyBeetlRenderFactory;
import net.bright.demo.ext.plugin.ConfigPlugin;
import net.bright.demo.ext.plugin.ControllerPlugin;
import net.bright.demo.ext.plugin.TablelPlugin;
import net.bright.demo.handler.BloglHandler;
import net.bright.demo.kit.CFKit;
import net.bright.demo.kit.ConfigKit;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;

/**
 * API引导式配置
 */
public class ProjectConfig extends JFinalConfig {

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		new ConfigPlugin(".*.txt").reload(false).start();
		
		me.setDevMode(ConfigKit.getBoolean(DictKeys.conf_devMode, false));
		me.setMainRenderFactory(new MyBeetlRenderFactory());
		
		me.setError404View("/error/404.html");
		me.setError401View("/error/401.html");
		me.setError403View("/error/403.html");
		me.setError500View("/error/500.html");
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		//路由扫描注册
		new ControllerPlugin(me).start();
	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {

		String[] dbConfigs = ConfigKit.getStr(DictKeys.db_configs).split(",");
		if (dbConfigs.length == 0) {
            throw new RuntimeException("Unable to find any database config. ");
        }
		
		//可配置多数据源
		Map<String, ActiveRecordPlugin> arpMap = CFKit.newMap();
		
		int initialSize = ConfigKit.getInt(DictKeys.db_initialSize, 50);
		int minIdle = ConfigKit.getInt(DictKeys.db_minIdle, 50);
		int maxActive = ConfigKit.getInt(DictKeys.db_maxActive, 100);
		
		for (String config : dbConfigs) {
			String dbType = ConfigKit.getStr(getKey(config, DictKeys.db_type));
			String jdbcUrl = ConfigKit.getStr(getKey(config, DictKeys.db_jdbcUrl));
			String user = ConfigKit.getStr(getKey(config, DictKeys.db_user));
			String password = ConfigKit.getStr(getKey(config, DictKeys.db_password));
			String driverClass = ConfigKit.getStr(getKey(config, DictKeys.db_driverClass), null);
			DruidPlugin druidPlugin = null;
			if (driverClass != null) {
				druidPlugin = new DruidPlugin(jdbcUrl, user, password);
			} else {
				druidPlugin = new DruidPlugin(jdbcUrl, user, password, driverClass);
			}
			druidPlugin.set(initialSize, minIdle, maxActive);
			me.add(druidPlugin);
			
			ActiveRecordPlugin arp = new ActiveRecordPlugin(config, druidPlugin);
			if (dbType.equalsIgnoreCase("mysql")) {
				arp.setDialect(new MysqlDialect());
			} else if (dbType.equalsIgnoreCase("oracle")) {
				arp.setDialect(new OracleDialect());
				arp.setContainerFactory(new  CaseInsensitiveContainerFactory());
			} else if (dbType.equalsIgnoreCase("postgresql")) {
				arp.setDialect(new PostgreSqlDialect());
			}
			me.add(arp);
			arpMap.put(config, arp);
		}
		new TablelPlugin(arpMap).start();
	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		// 支持在jfinal中用sesion
		me.add(new SessionInViewInterceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new BloglHandler());
	}
	
	private String getKey(String config, String key) {
		return String.format("%s.%s", config, key);
	}

	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目 运行此 main
	 * 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) throws Exception {

		JFinal.start("WebRoot", 8080, "/", 5);
	}
}
